package com.cu.employmentmanagementsystem.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.cu.employmentmanagementsystem.common.exception.CommonException;
import com.cu.employmentmanagementsystem.common.result.R;
import com.cu.employmentmanagementsystem.entity.RecruitInformation;
import com.cu.employmentmanagementsystem.entity.StudentVita;
import com.cu.employmentmanagementsystem.entity.User;
import com.cu.employmentmanagementsystem.service.StudentVitaService;
import com.cu.employmentmanagementsystem.service.UserService;
import com.cu.employmentmanagementsystem.util.CommonUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("vita")
@SaCheckLogin
public class StudentVitaController {
    @Autowired
    private StudentVitaService  studentVitaService;

    @Autowired
    private UserService userService;
    /**
     * 查看自己简历的审核状态
     * @return 查询结果
     */
    @RequestMapping("checkVitaStatus")
    @ApiOperation(value = "查看自己的简历")
    public R<StudentVita> checkVitaStatus(){
        try{
            Long studentId = CommonUtils.getStudentId();
            //根据用户id得到唯一简历*
            StudentVita s = studentVitaService.selectByStudentId(studentId);
            return R.success(s);

        }catch (Exception e){
            throw new CommonException("查询个人简历的过程中出现异常");
        }
    }
    @PostMapping("commitVita")
    @ApiOperation(value = "发布简历")
    public R commitVita(@ApiParam(value = "学生简历信息")
            @RequestBody StudentVita studentVita){
        try{
            return studentVitaService.commitVita(studentVita);
        }catch (Exception e){
            throw new CommonException("提交简历的过程中出现异常！！！");
        }
    }

    @RequestMapping("updateVita")
    @ApiOperation(value = "更新简历，更新后需要重新审核")
    public R updateVita(@ApiParam(value = "学生简历信息") @RequestBody StudentVita studentVita){
        try{
            return studentVitaService.updateVita(studentVita);
        }catch (Exception e){
            throw new CommonException("更新简历的过程中出现异常");
        }
    }

    @GetMapping("getAllUnJudgeVitas")
    @ApiOperation(value = "获取所有未审核的简历")
    public R<List<StudentVita>> getAllUnJudgeVitas(){
        try{
            return R.success(studentVitaService.getAllUnJudgeVitas());
        }catch (Exception e){
            throw new CommonException("获取所有未审核简历的过程中出现异常");
        }
    }

    @GetMapping("getAllUnJudgeRecruits")
    @ApiOperation(value = "获取所有未审核的招聘信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "获取成功"),
            @ApiResponse(code = 500, message = "获取失败")
    })
    public R<List<RecruitInformation>> getAllUnJudgeRecruits(){
        try{
            return R.success(studentVitaService.getAllUnJudgeRecruits());
        }catch (Exception e){
            throw new CommonException("获取所有未审核招聘信息过程中出现异常");
        }
    }

    /**
     * 审核简历信息
     * @return 审核操作是否成功
     */
    @RequestMapping("processVita")
    @ApiOperation(value = "审核简历信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "审核简历成功"),
            @ApiResponse(code = 500, message = "审核简历失败")
    })
    public R processVita(@ApiParam(value = "学生简历", name="StudentVita") @RequestBody StudentVita studentVita){
        //存在问题，没有指定简历id
        //简历id的获取只能通过前端确定，不能直接传参也不能account获得userid（这个userid是当前登录的用户，即管理员）
        Long adminId = CommonUtils.getAdminId();
        if(adminId == null){
            return R.error("管理员权限，请登录");
        }
        try {
            int x = studentVitaService.updatePeviewStatus(studentVita);
            //审核成功
            if(x == 1 )
                return R.success(null);
            //审核失败
            return R.error("更新简历审核状态失败");
        }catch (Exception e){
            throw new CommonException("审核简历过程出现异常");
        }
    }
    /**
     * 审核招聘信息
     * @return 审核操作是否成功
     */
    @RequestMapping("processRecruit")
    @ApiOperation(value = "审核招聘信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "审核招聘成功"),
            @ApiResponse(code = 500, message = "审核招聘失败")
    })
    public R processRecruit(@ApiParam(value = "招聘信息", name= "RecruitInformation") @RequestBody RecruitInformation recruitInformation){
        //存在问题，没有指定公司招聘信息id
        //公司招聘信息id的获取只能通过前端确定，不能直接传参也不能account获得userid（这个userid是当前登录的用户，即管理员）
        Long adminId = CommonUtils.getAdminId();
        if(adminId == null){
            return R.error("管理员权限，请登录");
        }
        try {
            int x = studentVitaService.updatePeviewStatus2(recruitInformation);
            //审核成功
            if (x == 1 )
                return R.success(null);
            //审核失败
            return R.error("更新招聘审核状态失败");
        }catch (Exception e){
            throw new CommonException("审核招聘信息过程出现异常");
        }
    }
}
